package com.example.studentmanagement;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.studentmanagement.dao.CurriculumDao;
import com.example.studentmanagement.entity.Curriculum;

public class AddCurriculum extends AppCompatActivity {
    private CurriculumDao curriculumdao ;
    private EditText curriculum_name , curriculum_id , curriculum_teacher , curriculum_class , curriculum_time ;
    private Button addCurriculum ;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addcurriculum);

        curriculum_name = findViewById(R.id.curriculumName) ;
        curriculum_id = findViewById(R.id.curriculumId) ;
        curriculum_teacher = findViewById(R.id.curriculumTeacher) ;
        curriculum_class = findViewById(R.id.curriculumClass) ;
        curriculum_time = findViewById(R.id.curriculumTime) ;
        curriculumdao = new CurriculumDao();
    }

        public void addCurriculum(View view){
        String curriculumName = curriculum_name.getText().toString().trim();
        String curriculumId = curriculum_id.getText().toString().trim();
        String curriculumTeacher = curriculum_teacher.getText().toString().trim();
        String curriculumClass = curriculum_class.getText().toString().trim();
        //添加校验
        int curriculumTime = 0;
        try {
            curriculumTime = Integer.parseInt(curriculum_time.getText().toString().trim());
        } catch (NumberFormatException e) {
            // 处理解析失败的情况，比如显示错误提示给用户
            Toast.makeText(this, "上课时间必须是有效的整数", Toast.LENGTH_SHORT).show();
            return; // 或者采取其他合适的错误处理方式，比如结束该方法执行
        }

// 非空校验
        if (TextUtils.isEmpty(curriculumName)) {
            Toast.makeText(this, "课程名称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(curriculumId)) {
            Toast.makeText(this, "课程ID不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(curriculumTeacher)) {
            Toast.makeText(this, "授课教师不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(curriculumClass)) {
            Toast.makeText(this, "班级信息不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        Curriculum curriculum = new Curriculum(curriculumName , curriculumId , curriculumTeacher , curriculumClass , curriculumTime);
        long add = curriculumdao.addCurriculum(curriculum);
        if (add != -1){
            Toast.makeText(this, "课程添加成功", Toast.LENGTH_SHORT).show();
            onBackPressed();
        }else {
            Toast.makeText(this, "课程添加失败", Toast.LENGTH_SHORT).show();
        }
        }

}